package com.slm.krstartimes.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.slm.krstartimes.R;
import com.slm.krstartimes.adapter.CommentAdapter;
import com.slm.krstartimes.utils.Comment;
import com.slm.krstartimes.utils.L;
import com.slm.krstartimes.utils.MyStringRequest;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/8/12.
 */
public class CommentActivity extends Activity implements View.OnTouchListener, GestureDetector.OnGestureListener {
    private Intent intent;
    private LinearLayout comment_layout;
    private GestureDetector mGestureDetector;
    private int verticalMinDistance = 30;
    private int middleDistance = 150;
    private int minVelocity = 0;
    private ListView commentListView;
    private CommentAdapter commentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comment);

        initView();

        intent = getIntent();
        String newsId = intent.getStringExtra("newsId");
        newsId = newsId.substring(0, 3);
        getData(newsId);
    }

    /**
     * 获取网络数据
     *
     * @param newsId
     */
    private void getData(String newsId) {
        MyStringRequest stringRequest = new MyStringRequest("http://www.ithome.com/json/commentlist/" + newsId + "/491dddc31a0e6e60.json",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        if (s != null && s.length() > 2) {
                            s = s.substring(12, s.length() - 1);
                            if (s.indexOf("%") != -1) {

                                s = s.replace("%", "\\");
                            }
                            Gson gson = new Gson();// 实例化gson对象
                            Type type = new TypeToken<ArrayList<Comment>>() {
                            }.getType();
                            List<Comment> comments = gson.fromJson(s, type);
                            for (Comment comment : comments) {

                                L.i("评论列表:" + comment.getSF());

                            }
                            commentAdapter = new CommentAdapter(CommentActivity.this, comments);
                            commentListView.setAdapter(commentAdapter);
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                L.i("网络拉取", "评论列表:" + volleyError);
            }
        });
        MainActivity.requestQueue.add(stringRequest);
    }

    /**
     * 初始化
     */
    private void initView() {
        comment_layout = (LinearLayout) findViewById(R.id.comment_layout);
        commentListView = (ListView) findViewById(R.id.commentListView);
        commentListView.setOnTouchListener(this);
        commentListView.setLongClickable(true);
        comment_layout.setOnTouchListener(this);
        comment_layout.setLongClickable(true);
        mGestureDetector = new GestureDetector(this);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return mGestureDetector.onTouchEvent(motionEvent);
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {

    }

    /**
     * 滑动返回
     *
     * @param e1
     * @param e2
     * @param v
     * @param v1
     * @return
     */
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float v, float v1) {
        if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(v) > minVelocity && (Math.abs(e2.getY() - e1.getY()) < middleDistance)) {
            L.i("滑动返回," + e2 + "," + e1);
            finish();
        }
        return false;
    }
}
